package cn.tedu._06notice.controller;

import ch.qos.logback.classic.spi.EventArgUtil;
import cn.tedu._06notice.base.response.JsonResult;
import cn.tedu._06notice.base.response.StatusCode;
import cn.tedu._06notice.mapper.NoticeMapper;
import cn.tedu._06notice.pojo.dto.NoticeAddParam;
import cn.tedu._06notice.pojo.dto.NoticeListQuery;
import cn.tedu._06notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._06notice.pojo.entity.Notice;
import cn.tedu._06notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._06notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * RestController注解:
 * 1.组合注解: 等价于 @Controller + @ResponseBody;
 *  *   2.简化开发: 在类上添加该注解后,会为每个方法自动添加 @ResponseBody 注解;
 *
 *  Slf4j注解:
 *  1.
 *  2.
 *  日志对象log:
 *  1.log.trace();
 *  2.log.debug();
 *  3.log.info();
 *  4.log.warn();
 *  5.log.error();
 */
@Slf4j
@RestController
@Tag(name="01-资讯管理模块")
@RequestMapping("/v1/notice/")
public class NoticeController {
    //自动装备
    @Autowired
    private NoticeMapper noticeMapper;
    /**
     * 1.添加咨询功能
     */
    @PostMapping("add")
    @Operation(summary = "添加资讯")
    @ApiOperationSupport(order = 10)
    @Parameters(value = {
            @Parameter(name = "title",description = "资讯标题"),
            @Parameter(name = "type",description = "资讯类型"),
            @Parameter(name = "content",description = "资讯内容"),
            @Parameter(name = "status",description = "资讯状态"),
            @Parameter(name = "noticeAddParam",hidden = true)
    })
    public JsonResult addNotice( NoticeAddParam noticeAddParam) {
        log.debug("noticeAddParam = " + noticeAddParam);
        //终极目标:调用接口方法,将数据存入数据表中;
        Notice notice=new Notice();
        //复制属性
        BeanUtils.copyProperties(noticeAddParam,notice);
        log.debug("notice = " + notice);
        //处理其他属性
        notice.setUserId(1L);//暂时定死,未来一定是登录的用户的真是ID;
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        notice.setIsDelete(0);
        log.debug("notice = " + notice);
        noticeMapper.insertNotice(notice);

        return  JsonResult.ok();
    }
    /**
     * 2.资讯列表功能;
     */
    @GetMapping("list")
    @Operation(summary = "资讯列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list(NoticeListQuery noticeListQuery){
        log.debug("资讯列表业务 " + noticeListQuery);
        List<NoticeListVO> noticeListVOS = noticeMapper.selectNoticeByTitleAndType(noticeListQuery);
        return JsonResult.ok(noticeListVOS);
    }

    /**
     * 3.咨询详情功能;
     */
    @GetMapping("detail")
    @Operation(summary = "资讯详情")
    @ApiOperationSupport(order = 30)
    @Parameters(value = {
            @Parameter(name = "id",description = "资讯ID",required = true),
            @Parameter(name = "username",description = "用户名",required = true,hidden = true)
    })

    public JsonResult detail(Long id,String username) {
        //此处username参数无其他作用,只是为了掩饰Knife4j中参数
       log.debug("id = " + id);
        //Alt+Enter:选择
        NoticeDetailInfoVO noticeDetailInfoVO=noticeMapper.selectNoticeById(id);
        return  JsonResult.ok(noticeDetailInfoVO);
    }

    /**
     *4.删除资讯功能
     *
     */
    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)
    public JsonResult delete(Long id) {
        log.debug("id = " + id);
        int rows =noticeMapper.deleteNoticeById(id);
        if(rows>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @PutMapping("update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 50)
    public JsonResult update(NoticeUpdateParam noticeUpdateParam) {
        log.debug("noticeUpdateParam = " + noticeUpdateParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeUpdateParam,notice);
        notice.setUpdateTime(new Date());
        //调用接口方法
        int rows = noticeMapper.updateNoticeById(notice);
        if(rows>0){
            return JsonResult.ok();

        } else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }
}
